package com.example.controller;

import com.example.Dto.*;
import com.example.Result.Result;
import com.example.Vo.LikeEachOtherVo;
import com.example.Vo.LoginVo;
import com.example.Vo.UserAnswerVo;
import com.example.pojo.User;
import com.example.pojo.UserAnswerProblem;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * @author liguoping
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 发送验证码
     *
     * @param userDto 接受的数据集合
     * @return 结果
     */

    @PostMapping("/sendMsg")
    public Result sendMsg(@RequestBody UserDto userDto) {
        boolean sendmsg = userService.sendMsg(userDto);
        if (sendmsg) {
            return Result.success();
        }
        return Result.error("发送失败");
    }

    /**
     * 登入
     *
     * @param userCodeDto 接受的数据集合
     * @return 结果
     */
    @PostMapping("login")
    public Result login(@RequestBody UserCodeDto userCodeDto) {
        LoginVo login = userService.login(userCodeDto);
        return Result.success(login);
    }

    /**
     * 修改个人信息
     * * @param user   用户信息集合
     * * @return  结果
     * * @throws ParseException   时间异常处理
     */
    @PutMapping("update")
    public Result updateUser(@RequestBody User user) throws ParseException {
        boolean res = userService.updateUser(user);
        if (res) {
            return Result.success();
        }
        System.out.println("惠超真的是最小最细的");
        return Result.error("新增资料失败");
    }

    /**
     * 校验验证码
     *
     * @param userCodeDto 接受的数据集合
     * @return 校验结果
     */
    @PostMapping("/updatePhoneSendMsg")
    public Result updatePhoneSendMsg(@RequestBody UserCodeDto userCodeDto) {
        boolean res = userService.updatePhoneSendMsg(userCodeDto);
        if (res) {
            return Result.success("验证成功！");
        }
        return Result.error("验证失败！");
    }

    /**
     * 修改手机号
     *
     * @param NewPhone 新的手机号
     * @return 结果
     */
    @PutMapping("updatePhone")
    public Result updatePhone(@RequestParam String NewPhone) {
        boolean res = userService.updatePhone(NewPhone);
        if (res) {
            return Result.success("修改密码成功");
        }
        return Result.error("修改密码失败");
    }

    /**
     * 添加陌生人问题
     *
     * @param userProblemDto 问题介绍
     * @return 结果
     */
    @PostMapping("addProblem")
    public Result addProblem(@RequestBody UserProblemDto userProblemDto) {
        boolean res = userService.addProblem(userProblemDto);
        if (res) {
            return Result.success("添加问题成功");
        }
        return Result.error("添加问题失败");
    }

    /**
     * 回答陌生人问题
     *
     * @param userAnswerProblemDto 接受数据集合
     * @return 结果
     */
    @PostMapping("answerProblem")
    public Result addAnserProblem(@RequestBody UserAnswerProblemDto userAnswerProblemDto) {
        boolean res = userService.addAnswerProblem(userAnswerProblemDto);
        if (res) {
            return Result.success("回答成功");
        }
        return Result.error("回答失败");
    }

    /**
     * 查询陌生人问题答案
     *
     * @return 结果
     */
    @GetMapping("getAnswerProblem")
    public Result findAnswerProblemById() {
        UserAnswerVo userAnswerVo = userService.findAnswerProblemById();
        return Result.success(userAnswerVo);
    }

    /**
     * * 用户通知设置
     * * @param like  允许给我推送喜欢通知           0：关闭    1：开启
     * * @param comment   允许给我推送评论通知       0：关闭    1：开启
     * * @param announcement  允许给我推送公告通知   0：关闭    1：开启
     * * @return  结果
     */
    @PutMapping("install")
    public Result updateInstall(@RequestParam(defaultValue = "0") Integer like,
                                @RequestParam(defaultValue = "0") Integer comment,
                                @RequestParam(defaultValue = "0") Integer announcement) {

        boolean res = userService.updateInstall(like, comment, announcement);
        if (res) {
            return Result.success("设置成功");
        }
        return Result.error("设置失败");
    }

    /**
     * 添加喜欢
     *
     * @param userLikeDto 被喜欢的id
     * @return 结果
     */
    @PostMapping("setLike")
    public Result setLike(@RequestBody UserLikeDto userLikeDto) {
        boolean res = userService.setLike(userLikeDto);
        if (res) {
            return Result.success("喜欢成功");
        }
        return Result.error("喜欢失败");
    }

    /**
     * 查询互相喜欢
     *
     * @param id 用户id
     * @return 结果
     */
    @GetMapping("getLikeEachOther")
    public Result getLikeEachOther(@RequestParam String id) {
        List<LikeEachOtherVo> likeEachOtherVo = userService.getLikeEachOther(id);
        return Result.success(likeEachOtherVo);
    }

    /**
     * 取消喜欢
     *
     * @param likeId 需要去喜欢者的id
     * @return 结果
     */
    @PostMapping("cancelLike")
    public Result cancelLike(@RequestParam String likeId) {
        boolean res = userService.cancelLike(likeId);
        if (res) {
            return Result.success("取消成功");
        }
        return Result.error("取消失败");
    }

    /**
     * 查询喜欢
     *
     * @return 结果
     */
    @GetMapping("getLike")
    public Result getLike() {
        List<LikeEachOtherVo> likeEachOtherVos = userService.getLike();
        return Result.success(likeEachOtherVos);
    }

    /**
     * 查询粉丝
     *
     * @return 结果
     */
    @GetMapping("getFans")
    public Result getFans() {
        List<LikeEachOtherVo> likeEachOtherVos = userService.getFans();
        return Result.success(likeEachOtherVos);
    }
}